<?php
namespace app\index\controller;

use think\Controller;
use app\index\model\Project as Projects;
use app\index\model\Team;
use app\index\model\Member;
use app\index\model\Addonarticle;
use app\index\model\Archive;
use app\index\model\Order;
use app\firm\model\ProjectUser;

class Project extends Common
{
    public function _initialize(){
        parent::_initialize();
		if(!CheckMemberLogin()){
			$this->redirect('Login/index');
		}
		if(session('isCheck')!=1){
			$this->redirect('member/authentication');
		}
    }
    
    public function index()
    {
		$pmodel = new Projects;
		$status = input('status');
		if($status){
			$where['p.status'] = $status == 3 ? ['>=',3] : $status;
		}else{
			$where['p.status'] = ['>',0];
		}
		if(input('search')){
			$where['p.project_name|p.description|p.keywords'] = ['like','%'.input('search').'%'];
		}
		$field="p.*,pu.company_name";
		if($_SERVER['REMOTE_ADDR']!='118.116.104.88'){
			$where['p.id']= ['not in',[30,35]];
			$dataList = $pmodel
					->field($field)
					->alias('p')
					->join('tf_project_user pu','p.initiator=pu.id')->field($field)
					->where($where)
					->order('p.top desc,p.create_time desc')
					->paginate(9,false,['query'=>request()->param()]);
        }else{
			$dataList = $pmodel
					->field($field)
					->alias('p')
					->join('tf_project_user pu','p.initiator=pu.id')->field($field)
					->where($where)
					->order('p.top desc,p.create_time desc')
					->paginate(9,false,['query'=>request()->param()]);
		}


		$this->assign('dataList', $dataList);
		$this->assign('status',$status);
		$this->assign('StatusList',$this->StatusList());
		$this->assign('webtitle','项目中心 - 好融艺');
		return $this->fetch();
    }
	
	public function detail($id){
		if(!CheckMemberLogin()){
			$this->redirect('Login/index');
		}
		if(session('isCheck')!=1){
			$this->redirect('member/authentication');
		}

		$pmodel = new Projects;
		$tModel = new Team;
		$puModel = new ProjectUser();
		if($_SERVER['REMOTE_ADDR']!='218.88.26.143' && ($id==30 || $id==35)){
			$id=31;
			$data = $pmodel->where(['id'=>$id])->find()->toArray();
		}else{
			$data = $pmodel->where(['id'=>$id])->find()->toArray();
		}
        if(!empty($data['thumb'])){
            $img = explode(',',$data['thumb']);
            $data['thumb'] = $img;
        }
//		if($data['initiator'] > 0){
//			$initiator  = $tModel->where(['id'=>$data['initiator']])->find();
//			$this->assign('initiator', $initiator);
//		}
		if($data['initiator'] > 0){  //修改发行方数据来源
			$initiator  = $puModel->where(['id'=>$data['initiator']])->find()->toArray();
			$this->assign('initiator', $initiator);
		}
		//可用金额
		if(session('UID')){
			$mModel = new Member();
			$money = $mModel->where(['id'=>session('UID')])->value('money');
			$this->assign('money',$money);
			$orderModel = new Order();
                        $where['delete_time'] = ['EXP','IS NULL'];
			$hasMoney = $orderModel->where(['project_id'=>$id,'uid'=>session('UID'),'status'=>array('in',[1,2])])->where($where)->sum('money');
			$this->assign('hasMoney',$hasMoney);
		}
		//项目动态 
		$arcModel = new Archive();
		$newList = $arcModel->where(['status'=>1,'project_id'=>$data['id']])->select();
		$this->assign('newList',$newList);
		//风险说明
		$aModel = new Addonarticle();
		$fengxiangshuoming = $aModel->where(['aid'=>9])->value('content');
		$this->assign('fengxiangshuoming', htmlspecialchars_decode($fengxiangshuoming));
		$this->assign('data',$data);
        $this->assign('webtitle', $data['webtitle'] ? $data['webtitle'] . ' - 好融艺' : $data['project_name'] . ' - 好融艺');   
        $this->assign('keywords', $data['keywords']);   
        $this->assign('description', $data['description']);   
		return $this->fetch();
	}
	
	private function StatusList(){
		return [
			'0' => '全部',
			'1' => '预热中',
			'2' => '认投中',
			'3' => '已成功',
//			'-1' => '已结束' todo 隐藏
		];
	}
}
